package financial.sharing.center.contract.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.*;
import financial.sharing.center.common.utils.BigDecimalUtil;
import financial.sharing.center.common.utils.StringUtil;
import financial.sharing.center.contract.dao.ContractMapper;
import financial.sharing.center.contract.feign.InvoiceFeignTestService;
import financial.sharing.center.contract.feign.PayableFeignTestService;
import financial.sharing.center.contract.feign.ReceivableFeignTestService;
import financial.sharing.center.contract.feign.SystemFeignTestService;
import financial.sharing.center.contract.model.entity.Contract;
import financial.sharing.center.contract.model.entity.ContractDetail;
import financial.sharing.center.contract.service.ContractDetailService;
import financial.sharing.center.contract.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ContractServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/4/22
 * @Version 1.0
 */
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private ContractDetailService contractDetailServiceImpl;

    @Autowired
    private SystemFeignTestService systemFeignTestService;

    @Autowired
    private InvoiceFeignTestService invoiceFeignTestService;

    @Autowired
    private PayableFeignTestService payableFeignTestService;

    @Autowired
    private ReceivableFeignTestService receivableFeignTestService;

    @Override
    public PageInfo<Contract> selectAllContract(String selectContractName, String selectFirstParty, String selectSecondParty) {
        Example example = new Example(Contract.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        if (!StringUtil.isEmpty(selectContractName)){
            criteria.andLike("contractName", StringUtil.toLike(selectContractName));
        }
        if (!StringUtil.isEmpty(selectFirstParty)){
            criteria.andLike("firstParty", StringUtil.toLike(selectFirstParty));
        }
        if (!StringUtil.isEmpty(selectSecondParty)){
            criteria.andLike("secondParty", StringUtil.toLike(selectSecondParty));
        }
        List<Contract> contractList = this.contractMapper.selectByExample(example);
        Map<Integer,String> businessLineMap = this.systemFeignTestService.getMapBusinessLine();
        Map<Integer, BigDecimal> invoiceAmountMap = this.invoiceFeignTestService.getMapInvoice();
        Map<Integer, BigDecimal> receivableAccruedAmountMap = this.receivableFeignTestService.getMapReceivableAccrued();
        Map<Integer, BigDecimal> receivableBillAmountMap = this.receivableFeignTestService.getMapReceivableBill();
        Map<Integer, BigDecimal> payableAccruedAmountMap = this.payableFeignTestService.getMapPayableAccrued();
        Map<Integer, BigDecimal> payableBillAmountMap = this.payableFeignTestService.getMapPayableBill();
        for (Contract contract : contractList) {
            contract.setBusinessLineName(businessLineMap.get(contract.getBusinessLineId()));
            contract.setContractDetail(this.contractDetailServiceImpl.getOneContractDetail(contract));
            contract.getContractDetail().setInvoiceAmount(invoiceAmountMap.get(contract.getId()) == null ? BigDecimalUtil.ZERO : invoiceAmountMap.get(contract.getId()));
            if (ContractDirectionEnum.收入合同.getValue().equals(contract.getContractDirectionValue())){
                contract.getContractDetail().setAccruedAmount(receivableAccruedAmountMap.get(contract.getId()) == null ? BigDecimalUtil.ZERO : receivableAccruedAmountMap.get(contract.getId()));
                contract.getContractDetail().setBillAmount(receivableBillAmountMap.get(contract.getId()) == null ? BigDecimalUtil.ZERO : receivableBillAmountMap.get(contract.getId()));
            }
            else if (ContractDirectionEnum.支出合同.getValue().equals(contract.getContractDirectionValue())){
                contract.getContractDetail().setAccruedAmount(payableAccruedAmountMap.get(contract.getId()) == null ? BigDecimalUtil.ZERO : payableAccruedAmountMap.get(contract.getId()));
                contract.getContractDetail().setBillAmount(payableBillAmountMap.get(contract.getId()) == null ? BigDecimalUtil.ZERO : payableBillAmountMap.get(contract.getId()));
            }
            contract.setContractDirectionName(ContractDirectionEnum.int2Enum(contract.getContractDirectionValue()).getName());
            contract.setContractStageName(ContractStageEnum.int2Enum(contract.getContractStageValue()).getName());
            contract.setContractTypeName(ContractTypeEnum.int2Enum(contract.getContractType()).getName());
        }
        return new PageInfo<>(contractList);
    }

    @Override
    public Integer createOneContract(String contractName, Integer contractTypeValue, Integer contractDirectionValue, String firstParty, String secondParty) {
        Contract contract = new Contract(contractName,contractTypeValue,contractDirectionValue);
        this.contractMapper.insertSelective(contract);
        ContractDetail contractDetail = new ContractDetail(contract.getId(),firstParty,secondParty);
        return this.contractDetailServiceImpl.createContractDetail(contractDetail);
    }

    @Override
    public Integer compileOneContract(Contract contract) {
        int i = this.contractMapper.updateByPrimaryKey(contract);
        Integer j = this.contractDetailServiceImpl.compileOneContractDetail(contract.getContractDetail());
        return i == 1 && j == ResponseContext.成功.getValue().intValue() ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOneContract(Integer id) {
        Contract contract = this.getContract(id);
        contract.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.contractMapper.updateByPrimaryKey(contract);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public Contract getContract(Integer id) {
        return this.contractMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Contract> getListContract(Integer contractDirectionValue) {
        Example example = new Example(Contract.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("enableStatusValue",EnableStatusEnum.正常.getValue());
        if (!ContractDirectionEnum.无.getValue().equals(contractDirectionValue)){
            criteria.andEqualTo("contractDirectionValue", contractDirectionValue);
        }
        List<Contract> contractList = this.contractMapper.selectByExample(example);
        return contractList;
    }
}
